<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
	//控制器初始化  TP执行构造函数里面执行的方法 [_initialize()]  Home模块里的所有方法都要执行该函数
	public function _initialize()
	{
		//var_dump(MODULE_NAME ,CONTROLLER_NAME ,ACTION_NAME );
		$path = MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
		if (in_array($path, C('AUTH_PATHS'))) {
			if (!isset($_SESSION['id'])) {
				$this->error('请先登录',U('home/login/index'),4);
			}
		}
	}

	//Jie调用 浏览量 回答量 赞 飞吻等自动增加 或 扣减的方法 $table[要操作的表名] $filed[字段名] $num[加或减的数值] $id[条件ID]
    public function autoAdd($table,$filed,$num,$id)
    {
        $sql = "update ".$table." set ".$filed."=".$filed.$num." where id='{$id}'";
        //var_dump($sql); update question set view_num=view_num+1 where id='1'
        M()->execute($sql);
    }

    //index\Jie调用 得到近期热议的 15条数据
    public function getTopAnswer()
    {
    	$sql = "select * from question order by answer_num desc limit 15";
    	$hotAnswer = M()->query($sql);
    	//直接分配变量  哪个控制器来调用就分配给那个控制器
    	$this->assign('hotAnswer',$hotAnswer);
    }

    //index\Jie调用 获取回答月榜 20条回帖熟最多的用户数据展示
    public function getTopUser()
    {	//一个月的回答量未处理
    	$sql = "select * from user order by answer_num desc limit 20";
    	$TopAnswerUser = M()->query($sql);
    	//直接分配变量  哪个控制器来调用就分配给那个控制器
    	$this->assign('TopAnswerUser',$TopAnswerUser);
    }

    //Jie调用 获取用户收藏帖子的 总帖子id集合
    public function getStoreQid()
    {
    	$sql = "select qid from store where uid='{$_SESSION['id']}'";
    	$storeNum = M()->query($sql);
    	
    	$storeQids = [];
    	foreach ($storeNum as $v) {
    		$storeQids[] = $v['qid'];
    	}
    	$this->assign('storeQids',$storeQids);
    	// var_dump($storeQids);
    	
    	// if(in_array('8', $storeQids)){
    	// 	var_dump('YYYY');
    	// }else{
    	// 	var_dump('NNNN');
    	// }
    }

    //Jie调用 获取用户赞过的 总回复id集合
    public function getZanAid()
    {
    	$sql = "select aid from zan where uid='{$_SESSION['id']}'";
    	$zanNum = M()->query($sql);

    	$zanAids = [];
    	foreach ($zanNum as $v) {
    		$zanAids[] = $v['aid'];
    	}
    	$this->assign('zanAids',$zanAids);
    }


}